package com.bbz.generator.core.action.config;

import java.util.List;

import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.TemplateConfig;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
/**
 * 代码生成的抽象配置
 *
 * @author binbin.a.zhang
 * @date 2017-05-22
 */
public abstract class AbstractGeneratorConfig {
	/**
	 * mybatis-plus代码生成器配置
	 */
	GlobalConfig globalConfig = new GlobalConfig();

	DataSourceConfig dataSourceConfig = new DataSourceConfig();

	StrategyConfig strategyConfig = new StrategyConfig();

	PackageConfig packageConfig = new PackageConfig();

	TemplateConfig templateConfig = new TemplateConfig();

	TableInfo tableInfo = null;

	protected abstract void config();

	public void init() {
		config();
	}

	public AbstractGeneratorConfig() {
	}

	public void doMpGeneration() {
		init();
		AutoGenerator autoGenerator = new AutoGenerator();
		autoGenerator.setGlobalConfig(globalConfig);
		autoGenerator.setDataSource(dataSourceConfig);
		autoGenerator.setStrategy(strategyConfig);
		autoGenerator.setPackageInfo(packageConfig);
		autoGenerator.setTemplate(templateConfig);
		autoGenerator.execute();

		// destory();

		// 获取table信息,用于代码生成
		List<TableInfo> tableInfoList = autoGenerator.getConfig().getTableInfoList();
		if (tableInfoList != null && tableInfoList.size() > 0) {
			this.tableInfo = tableInfoList.get(0);
		}
	}

}
